<template>
  <div class="tab-control">
    <!-- index === currentIndex 的结果是 true，就会激活 active 这个样式 -->
    <div class="tab-control-item" :class="{active: index === currentIndex}" v-for="(item, index) in titles" 
    :key="index"
    @click="itemClick(index)"
    >
      <span>{{item}}</span>
    </div>
    

  </div>
</template>

<script>
import { ref } from '@vue/reactivity'
export default {
  name: 'TabControl',
  props: {
    // 接收 Home 组件传过来的标题名称数组
    titles: {
      type: Array,
      default() {
        return []
      }
    }
  },
  setup(props, {emit}) {
    // 选中的索引
    let currentIndex = ref(0)
    
    // 控制点击选项卡
    const itemClick = (index) => {
      currentIndex.value = index
      // 将 index 值，传给 Home 组件
      emit('tabClick', index)

    }

    
    return {
      currentIndex,
      itemClick
    }
  }
}
</script>

<style lang="scss" scpoed>
.tab-control {
  display: flex;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 14px;
  background-color: #fff;
  z-index: 10;
  // css3新增样式，滚动到距离顶部 44px 就固定
  position: sticky;
  top: 44px;
  
  .tab-control-item {
    flex: 1;
    span {
      padding: 5px;
    }
  }
  .active {
    color: var(--color-tint);
    span {
      border-bottom: 3px solid var(--color-tint);
    }
  }
}
</style>